    import { getCartesian3 } from '../BQGIS-transformation'



    /**
     *
     * 绕点旋转
     *
     * @param    viewer     Mars3d实例对象
     * 
     * @function start 开始旋转
     * @function stop  停止旋转
     */
    

    export function BQGIS_RotatePoint({viewer = window.viewer}) {
        this.viewer = viewer
        this.handler = null
    }

    /**
     *
     * 开始
     *
     * @param    position   坐标
     * @param    pitch      相机看点的角度
     * @param    distance   飞行半径
     * @param    time       飞行一周所需时间
     * @param    duration   飞行时长,若不传则不会停止
     * 
     * @returns  BQGIS_cameraDblClickMove
     *
     */

    BQGIS_RotatePoint.prototype.start = function ({position, pitch = 10, time = 10, distance = 5000, duration}) {
        this.stop()
        // 每秒转动度数
        const angle = 360 / time;
        const startTime = Cesium.JulianDate.fromDate(new Date());
        this.viewer.clock.startTime = startTime.clone(); // 开始时间
        if(duration != null && typeof duration === 'number'){
            this.viewer.clock.stopTime = Cesium.JulianDate.addSeconds(startTime, duration, new Cesium.JulianDate());
        }
        this.viewer.clock.currentTime = startTime.clone(); // 当前时间
        this.viewer.clock.clockRange = Cesium.ClockRange.CLAMPED; // 行为方式
        this.viewer.clock.clockStep = Cesium.ClockStep.SYSTEM_CLOCK; // 时钟设置为当前系统时间; 忽略所有其他设置。
        // 相机的当前heading
        const initialHeading = this.viewer.camera.heading;
        const exection = function () {
            // 当前已经过去的时间，单位s
            const delTime = Cesium.JulianDate.secondsDifference(this.viewer.clock.currentTime, this.viewer.clock.startTime);
            const heading = Cesium.Math.toRadians(delTime * angle) + initialHeading;
            this.viewer.scene.camera.setView({
                destination: getCartesian3(position), // 点的坐标
                orientation: {
                    heading: heading,
                    pitch: -pitch,

                }
            });
            this.viewer.scene.camera.moveBackward(distance);
            if (duration && Cesium.JulianDate.compare(this.viewer.clock.currentTime, this.viewer.clock.stopTime) >= 0) {
                this.stop()
            }
        };

        this.handler = this.viewer.clock.onTick.addEventListener(exection, this);
    }

    BQGIS_RotatePoint.prototype.stop = function () {
        if(this.handler){
            this.handler()
            this.handler = null
        }
    }